<?php
namespace app\controller;

use app\BaseController;
use app\model\Role as RoModel;
use think\facade\Db;
use app\model\RoleNode;
use app\model\Node;
use think\validate\ValidateRule;


class Role extends BaseController
{
    protected $middleware = [
        // 对所有方法有效
        '\app\middleware\NavCheck',
        '\app\middleware\UrlCheck',
        '\app\middleware\LoginCheck',
    ];
    public function index()
    {
        return view();
    }
    public function data(){
        $user = new RoModel();
        $limit = $this->request->param('limit');
        $page = $this->request->get('page');
        $offset = ($page-1)*$limit;
        $list = $user
            ->limit($offset,$limit)
            ->select();
        $count = RoModel::count();

        foreach ($list as $k=>$v){
            $list[$k]['node_name']  = Node::leftJoin('role_node rn','rn.node_id=node.id')
                ->where('rn.role_id',$v['id'])->column('node.node_name');
            //var_dump($node_list);
        }
        return json(["code"=>0,"msg"=>"查询成功","count"=>$count,"data"=>$list]);
    }

    public function add()
    {
        if($this->request->isPost()){
            $data = $this->request->post();
            $node_id = $data['node_id'];
            unset($data['node_id']);
            $user  = new RoModel();

            $res = $user->save($data);

            $role_id = $user->id;

            $r_n = [];
            foreach ($node_id as $k=>$v){
                $r_n[] = ['role_id'=>$role_id,
                    "node_id"=>$k,
                ];
            }
            $RN = new RoleNode();
            $res2 = $RN->saveAll($r_n);

            if($res){
                if($res2){
                    $this->success('添加成功','index');
                }
            }else{
                $this->error('添加失败');
            }
        }
        $list = Node::select();
        return view('',['list'=>$list]);
    }

    public function del()
    {
        $id=$this->request->post('id');
        $user = RoModel::find($id);
        $res = $user->delete();
        if($res){
            $this->success('删除成功','index');
        }else{
            $this->error('删除失败');
        }

    }
    public function update(){
        if($this->request->isPost()){
            $data = $this->request->post();
            $node_id = $data['node_id'];
            unset($data['node_id']);


           Db::table('role_node')->where('role_id',$data['id'])->delete();
            $nr = [];
            foreach ($node_id as $k=>$v){
                $nr[] = ['role_id'=>$data['id'],'node_id'=>$k];
            }


            $user = new RoleNode();
            $user->saveAll($nr);
            $ro_name = RoModel::where('role_name', $data['role_name'])->find();

            $RO =new RoModel();
            if(!empty($ro_name)){
                unset($data['role_name']);
            }
            //$id= $data['id'];
            $user = RoModel::find($data['id']);
            unset($data['id']);
            $res = $user->save($data);



            if($res){
                $this->success('修改成功','index');
            }else{
                $this->error('删除失败');
            }
        }
        $info = Node::select();
        $id=$this->request->param('id');
        $list = RoModel::find($id);
        $check = RoleNode::where('role_id',$id)->column('node_id');

        return view('update',['list'=>$list,'info'=>$info,'check'=>$check]);
    }
}
